// pages/circle/addcircle.js
const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: [],
    circletype: 0,
    img_url: [],
    content: '',
    isimg: true,
  },
  //分享动态
  input: function (e) { 
    this.setData({
       content: e.detail.value
     })
  },
  //pick圈子类型
  bindQgChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      circletype: e.detail.value
    })
  },
  //选择图片
  chooseimage:function(){
    var that = this;
    if(that.data.img_url.length == 0){
      var count = 9;
    }else{
      var count = 9 - that.data.img_url.length;
    }
      wx.chooseImage({
        count: count,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          if (res.tempFilePaths.length > 0) {
            //把每次选择的图push进数组
            var img_url = that.data.img_url;
            for (let i = 0; i < res.tempFilePaths.length; i++) {
              img_url.push(res.tempFilePaths[i])
            }
            that.setData({
              img_url: img_url,
              isimg: false
            })
            // console.log(that.data.img_url.length);
            if (that.data.img_url.length >= 9) {
              that.setData({
                hideAdd: 1
              })
            } else {
              that.setData({
                hideAdd: 0
              })
            }
          }
        },
        fail: function (res) {},
        complete: function (res) {},
      })
  },
  //发布按钮事件
  send:function(){
    var that = this;
    var user_id = app.globalData.openid;
    wx.showLoading({
      title: '上传中',
    })
    that.img_upload()
  },
  //图片上传
  img_upload:function(){
    var that = this;
    var img_url = that.data.img_url;
    console.log(img_url);
    var img_url_ok = [];
    
    for (let i = 0; i < img_url.length; i++) {
        wx.uploadFile({
          //路径填上传图片方法的地址
          url: app.globalData.serverUrl + 'photoupload',
          filePath: img_url[i],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: function (res) {
            console.log('上传成功');
            //把上传成功的图片的地址放入数组中
            const resData = JSON.parse(res.data);
            img_url_ok.push(resData.info);
            console.log(resData);
            //如果全部传完，则可以将图片路径保存到数据库 
            if (img_url_ok.length == img_url.length) {
              var userid = app.globalData.openid;
              var content = that.data.content;
              var circletype = that.data.circletype;
              wx.request({
                //路径为添加到数据库的方法
                url: app.globalData.serverUrl + 'photoadds',
                method: 'post',
                data: {
                  user_id: userid,
                  images: img_url_ok,
                  content: content,
                  circletype: circletype
                },
                success: function (res) {
                  //console.log(res);
                  if (res.data.status == 1) {
                    wx.hideLoading()
                    wx.showToast({
                      title: '上传成功',
                      icon: 'success',
                      duration: 2000,
                      success: function (res) {
                        // console.log(res);
                        setTimeout(function () {
                          wx.switchTab({
                            url: '/pages/circle/index',
                            success: function (e) {
                              // console.log(e);
                              var page = getCurrentPages().pop();
                              if (page == undefined || page == null) return;
                              page.onLoad();
                            }
                          })
                        }, 2000);
                      }
                    })
                  }else if(res.data.status == 2){
                    wx.hideLoading();
                    wx.showToast({
                      title: '请先登录',
                      icon: 'none',
                      duration: 2000
                    })
                  }else{
                    wx.hideLoading();
                    wx.showToast({
                      title: '上传失败，请重新尝试',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                },
                fail: function (res) {
                  if (res.data.status == 0) {
                    wx.hideLoading()
                    wx.showModal({
                      title: '提交失败，请重新提交',
                      showCancel: false,
                    })
                  }
                }
              })
            }
          }
        })
      } 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取圈子类型信息
    this.getNav();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取圈子类型信息
  getNav: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'navinfo',
      success: function (res) {

        console.log(res);
        if (res) {
          that.setData({
            array: res.data
          });
        }
      }
    })
  },
})